package com.sam.im.heiqun.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.sam.im.heiqun.R;


/**
 * @author gaobo 2018年12月12日
 * 对话框基类
 */
public abstract class BaseDialog extends Dialog {
    public static final int DIALOG_COMMON_STYLE = R.style.common_dialog_style;
    public static final int DIALOG_IOS_STYLE = R.style.dialog_ios_style;


    protected Context mContext;

    /**
     * 设置显示属性
     */
    private Display mDisplay;
    private Dialog mDialog;

    /**
     * 对话框样式id
     * @return
     */
    protected abstract int getDialogStyleId();

    /**
     * 获取view
     * @return
     */
    protected abstract View getView();


    public BaseDialog(Context context) {
        super(context);
        this.mContext = context;
        initData();
    }

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        initData();
    }

    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        initData();
    }
    private void initData(){
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mDisplay = windowManager.getDefaultDisplay();
        //在这里初始化 基础对话框
        if (getDialogStyleId() == 0){
            mDialog = new Dialog(mContext, DIALOG_COMMON_STYLE );
        }else {
            mDialog = new Dialog(mContext, getDialogStyleId());
        }
        mDialog.setContentView(getView());
        //隐藏系统输入盘
        mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    /**
     * 设置dialog弹出后会点击屏幕或物理返回键，dialog不消失
     * @param cancel
     * @return
     */
    public  BaseDialog setCancelDialog(boolean cancel){
        mDialog.setCancelable(cancel);
        return this;
    }

    /**
     * 设置dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
     * @param cancel
     * @return
     */
    public BaseDialog setCanceledOnTouchOutsideDialog(boolean cancel){
        mDialog.setCancelable(cancel);
        return this;
    }

    /**
     * 显示对话框
     */
    public void show() {
        mDialog.show();
    }

    /**
     * 取消对话框
     */
    public void dismiss(){
        mDialog.dismiss();
    }

    /**
     * 判断对话框是否显示
     * @return
     */
    public boolean isShowing(){
        return mDialog.isShowing();
    }

    /**
     * 对话框消失监听器
     * @param dismissListener
     * @return
     */
    public BaseDialog setdismissListeren(OnDismissListener dismissListener){
        mDialog.setOnDismissListener(dismissListener);
        return this;
    }

    /**
     * 设置居中弹窗的大小
     * @param width 宽高
     * @param height
     */
    public void setDialogSize(int width, int height){
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = width;
        lp.height = height;
        window.setAttributes(lp);
    }

    /**
     * 设置弹窗位置及大小
     * @param type 上下左右中
     * @param width 宽度
     * @param height 高度
     */
    public void setDialogSize(DialogLocationType type,int width,int height){
        Window window = mDialog.getWindow();
        switch (type) {
            case TOP:
                window.setGravity(Gravity.TOP);
                break;
            case LEFT:
                window.setGravity(Gravity.LEFT);
                break;
            case RIGHT:
                window.setGravity(Gravity.RIGHT);
                break;
            case BOTTOM:
                window.setGravity(Gravity.BOTTOM);
                break;
            case CENTER:
                window.setGravity(Gravity.CENTER);
                break;
        }
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = width;
        lp.height = height;
        window.setAttributes(lp);

    }
}
